import 'dart:async';

import 'package:flutter/material.dart';
import 'package:mc_card/dao/shequ/shequ_recommend_dao.dart';
import 'package:mc_card/model/shequ/article_item_model.dart';
import 'package:mc_card/model/shequ/banner_model.dart';
import 'package:mc_card/model/shequ/recommend_model.dart';
import 'package:mc_card/model/shequ/subject_model.dart';
import 'package:mc_card/model/shequ/topic_model.dart';
import 'package:mc_card/widget/loading_container.dart';
import 'package:mc_card/widget/shequ/banner_nav.dart';
import 'package:mc_card/widget/shequ/feed_nav.dart';
import 'package:mc_card/widget/shequ/topic_nav.dart';

///推荐聚合model
RecommendModel recommendModel;

///顶部Banner广告
List<BannerModel> banners;

///推荐话题
List<TopicModel> topics;

///page=1时候的feed流数据
List<ArticleItemModel> articles;

///精选专题
List<SubjectModel> feedSelections;

/// 是否在loading
var isLoading = true;
var parentContext;

/// 社区推荐页面，后面可以改成 推荐 、关注流
class SheQuApp extends StatelessWidget {
  SheQuApp(BuildContext pContent) {
    parentContext = pContent;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "推荐",
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: FeedsPage(),
    );
  }
}

class FeedsPage extends StatefulWidget {
  FeedsPage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _FeedsWidget createState() => _FeedsWidget();
}

class _FeedsWidget extends State<FeedsPage> {
  int page = 0;
  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      onRefresh: getData,
      // 自定义 loading组件
      child: LoadingContainer(
        isLoading: isLoading,
        child: ListView(
          children: getListWidget(),
        ),
      ),
    );
  }

  Future<Null> getData() async {
    isLoading = true;
    page = page + 1;
    recommendModel = await SheQuRecommendDao.fetch(page);
    banners = recommendModel.banners;
    topics = recommendModel.topics;
    articles = recommendModel.articles;
    feedSelections = recommendModel.feedSelections;
    setState(() {
      isLoading = false;
    });
  }
}

List<Widget> getListWidget() {
  List<Widget> widgets = new List<Widget>();
  // banner
  widgets.add(BannerNav(banners, parentContext));
  // 推荐话题
  widgets.add(TopicNav(topicList: topics));
  // feed流
  widgets.add(FeedItemNav(articles, parentContext));

  // 精选专题

  return widgets;
}
